﻿using System;
using fastCSharp;
using fastCSharp.emit;

namespace diantou.web.ajax.admin
{
    /// <summary>
    /// 问题调用
    /// </summary>
    [fastCSharp.code.cSharp.ajax(IsPool = true)]
    class problem : permissionHandle<problem>
    {
        /// <summary>
        /// 添加问题
        /// </summary>
        /// <param name="value"></param>
        /// <param name="onReturn"></param>
        public void Append(diantou.dataAccess.problem value, Action<fastCSharp.net.returnValue<int>> onReturn)
        {
            if ((currentUser.Permission & dataModel.user.permission.OnlineJudgeEditor) != 0)
            {
                value.UserId = currentUser.Id;
                diantou.dataAccess.tcpCall.problem.append(value, onReturn);
            }
            else onReturn(0);
        }
        /// <summary>
        /// 修改问题
        /// </summary>
        /// <param name="value"></param>
        /// <param name="onReturn"></param>
        public void Rework(diantou.dataAccess.problem value, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if ((currentUser.Permission & dataModel.user.permission.OnlineJudgeEditor) != 0) diantou.dataAccess.tcpCall.problem.rework(value, currentUser.Id, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 修改问题
        /// </summary>
        /// <param name="value"></param>
        /// <param name="onReturn"></param>
        public void ReworkJudgeSetting(diantou.dataAccess.problem value, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if ((currentUser.Permission & dataModel.user.permission.OnlineJudgeEditor) != 0) diantou.dataAccess.tcpCall.problem.reworkJudgeSetting(value, currentUser.Id, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 教程问题成员
        /// </summary>
        private static readonly fastCSharp.code.memberMap<diantou.dataModel.problem> getTutorialMemberMap = fastCSharp.code.memberMap<diantou.dataModel.problem>.CreateBuilder().Append(value => value.Id).Append(value => value.Title).Append(value => value.TestDataCount).Append(value => value.Description).Append(value => value.InputDescription).Append(value => value.OutputDescription).Append(value => value.Input).Append(value => value.Output);
        /// <summary>
        /// 获取问题标题
        /// </summary>
        /// <param name="id"></param>
        [fastCSharp.code.cSharp.ajax(IsOnlyPost = false)]
        public memberMapValue<diantou.dataModel.problem> GetTutorial(int id)
        {
            if ((currentUser.Permission & dataModel.user.permission.TutorialEditor) != 0)
            {
                diantou.dataAccess.problem problem = diantou.dataAccess.clientCache.Problem[id];
                if (problem != null && (problem.ShowType == dataModel.problemBase.showType.Show || problem.UserId == currentUser.Id))
                {
                    return new memberMapValue<diantou.dataModel.problem> { MemberMap = getTutorialMemberMap, Value = problem };
                }
            }
            return default(memberMapValue<diantou.dataModel.problem>);
        }
        /// <summary>
        /// 删除问题
        /// </summary>
        /// <param name="id"></param>
        /// <param name="onReturn"></param>
        public void Delete(int id, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if (currentUser.Permission == dataModel.user.permission.Admin) diantou.dataAccess.tcpCall.problem.delete(id, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 删除特判程序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="onReturn"></param>
        public void DeleteSpecialProgram(int id, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if (currentUser.Permission == dataModel.user.permission.Admin) diantou.dataAccess.tcpCall.problem.deleteSpecialProgram(id, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 问题ReJudge
        /// </summary>
        /// <param name="startId"></param>
        /// <param name="endId"></param>
        /// <param name="onReturn"></param>
        public void ReJudge(int startId, int endId, Action<fastCSharp.net.returnValue<int>> onReturn)
        {
            if (currentUser.Permission == dataModel.user.permission.Admin) diantou.dataAccess.tcpCall.judge.reJudgeProblem(startId, endId, onReturn);
            else onReturn(0);
        }
        /// <summary>
        /// 竞赛问题ReJudge
        /// </summary>
        /// <param name="contestId"></param>
        /// <param name="problemId"></param>
        /// <param name="onReturn"></param>
        public void ReContestJudge(int contestId, int problemId, Action<fastCSharp.net.returnValue<int>> onReturn)
        {
            if (currentUser.Permission == dataModel.user.permission.Admin) diantou.dataAccess.tcpCall.contestJudge.reJudgeProblem(contestId, problemId, onReturn);
            else onReturn(0);
        }
        /// <summary>
        /// 修改验题人
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <param name="onReturn"></param>
        public void ReworkTestUser(int id, int userId, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if ((currentUser.Permission & dataModel.user.permission.OnlineJudgeEditor) != 0) diantou.dataAccess.tcpCall.problem.reworkTestUser(id, userId, currentUser.Id, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 修改题解
        /// </summary>
        /// <param name="id"></param>
        /// <param name="solution"></param>
        /// <param name="onReturn"></param>
        public void ReworkSolution(int id, string solution, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if ((currentUser.Permission & dataModel.user.permission.OnlineJudgeEditor) != 0) diantou.dataAccess.tcpCall.problem.reworkSolution(id, solution, currentUser.Id, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 设置付款状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="onReturn"></param>
        public void SetPay(int id, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if (currentUser.Permission == dataModel.user.permission.Admin) diantou.dataAccess.tcpCall.problem.setPay(id, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 获取标程
        /// </summary>
        /// <param name="id"></param>
        /// <param name="onReturn"></param>
        public void GetProgram(int id, Action<fastCSharp.net.returnValue<diantou.dataModel.judgeState>> onReturn)
        {
            if ((currentUser.Permission & dataModel.user.permission.OnlineJudgeEditor) != 0) diantou.dataAccess.tcpCall.problem.getProgram(id, currentUser.Id, onReturn);
            else onReturn(null);
        }
        /// <summary>
        /// 获取验题程序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="onReturn"></param>
        public void GetTestProgram(int id, Action<fastCSharp.net.returnValue<diantou.dataModel.judgeState>> onReturn)
        {
            if (currentUser.Permission == dataModel.user.permission.Admin) diantou.dataAccess.tcpCall.problem.getTestProgram(id, onReturn);
            else onReturn(null);
        }
        /// <summary>
        /// 获取特判程序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="onReturn"></param>
        public void GetSpecialProgram(int id, Action<fastCSharp.net.returnValue<diantou.dataModel.judgeState>> onReturn)
        {
            if ((currentUser.Permission & dataModel.user.permission.OnlineJudgeEditor) != 0) diantou.dataAccess.tcpCall.problem.getSpecialProgram(id, currentUser.Id, onReturn);
            else onReturn(null);
        }
        /// <summary>
        /// 输入数据格式检测
        /// </summary>
        /// <param name="inputFormat"></param>
        /// <param name="onReturn"></param>
        public void CheckInputFormat(string inputFormat, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if ((currentUser.Permission & dataModel.user.permission.OnlineJudgeEditor) != 0) diantou.dataAccess.tcpCall.problem.checkInputFormat(inputFormat, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 刷新OJ数据
        /// </summary>
        /// <param name="startId"></param>
        /// <param name="endId"></param>
        /// <param name="onReturn"></param>
        public void RefreshJudge(int startId, int endId, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if (currentUser.Permission == dataModel.user.permission.Admin) diantou.dataAccess.tcpCall.problem.refreshJudge(startId, endId, onReturn);
            else onReturn(false);
        }
        /// <summary>
        /// 批量修改自动发布时间
        /// </summary>
        /// <param name="problemIds"></param>
        /// <param name="startTime"></param>
        public void BatchAutoShowTime(int[] problemIds, DateTime startTime, Action<fastCSharp.net.returnValue<int>> onReturn)
        {
            if (currentUser.Permission == dataModel.user.permission.Admin) diantou.dataAccess.tcpCall.problem.reworkAutoShowTime(problemIds, startTime, onReturn);
            else onReturn(0);
        }
    }
}
